#!/usr/bin/perl

use Data::Dumper;
use Getopt::Long;

$this   = $0;

$debug  = 0;

$res = GetOptions (
    "debug"    => \$debug   # flag
    # "length=i" => \$length, # numeric
    # "file=s"   => \$data,   # string
    # "verbose"  => \$verbose # flag
); 


$n_args = 1 + $#ARGV;


if ( $n_args >= 1 ) {
    @files = @ARGV;
} 
else {
    printf("Usage %s [-d] file_name\n", $0); 
    exit;
}




#
# Go through all the files and extract info
#

$n_files = 0;

foreach $file ( @files ) {


printf("Processing file %s\n", $file) if ($debug);

$n_files++;


#
#  1. Extract data from this file
#
#open FILE, ">$file" or die $!;
open FILE, $file or die $!;

$i = 0;
$mean_idx      = 0;
$header_passed = 0;


#
# 1.1 Get the file contents
#
my @lines = <FILE>;
#print @lines;

%data = ();
%case = ();


#while (my $line = <FILE>) { 
foreach my $line ( @lines )  { 

  $i += 1;
  
  #
  # 1.2 Extract the column headers
  #
  if ( $line =~ /Operation/ ) {

      $header_passed = 1;

      print $i, " MATCH", $line if ($debug);

      $line =~ s/Std Dev/Std-Dev/;

      # Remove leading and trailing spaces
      $line =~ s/^\s+//;
      $line =~ s/\s+$//;

      #print "JOIN", join(':', split('\s+', $line)), "\n";
      @tokens = split('\s+', $line);
      $tokenz = join(':', @tokens);
      print "JOIN\t", $tokenz, "\n" if ($debug);

      #foreach $token ( @tokens ) {
      #  print "token: ", $token, "\n";
      #}

  }

  elsif ( $line =~ /(\d+).*tasks,\s*(\d+)\s*files\/directories/ ) {

      #
      # Extract number of tasks and numbe rof files/dirs 
      #
      $n_tasks = $1;
      $n_files_dirs = $2;
      $case{"Num tasks"}      = $n_tasks; 
      $case{"Num files/dirs"} = $n_files_dirs;

      printf("# NTASKS = %s num_files = %s %s\n", $n_tasks, $n_files_dirs) if($debug);

  }
  elsif ( $line =~ /--/ ) {

      #
      # 1.3 Line to skip
      #
      print $i, " SKIP", $line if ($debug);

  }

  elsif (  $header_passed == 1 ) {

      #
      # 1.4 Line with data
      #
      $line =~ s/^\s+//;
      $line =~ s/\s+$//;

      if ( $line eq "" ) {
	  $line = " EMPTY";
      }

      print $i, " DATA ", $line, "\n" if ($debug);

      $line =~ s/Directory /Dir-/;
      $line =~ s/File /File-/;
      $line =~ s/Tree /Tree-/;
      $line =~ s/creation/create/;
      $line =~ s/removal/remove/;
      $line =~ s/://;

      @tokens = split('\s+', $line);
      if ( $tokens[0] =~ /Dir/ || 
	   $tokens[0] =~ /File/      || 
	   $tokens[0] =~ /Tree/ ) {
	  $data{$tokens[0]} = $tokens[3];
	  #print $i, " DATAX = ", $tokens[3], "\n";
      }

  }

  else {

      # 
      # 1.5 Header line
      #
      print $i, " HDER  ", $line if ($debug);
  }

} # foreach $line



#
# Dump the result
#
#print Dumper(\%data);



#
# 2. Generate report
#

#
# 2.0 Caption
#
if ( $n_files == 1 ) {
  printf ("\n\n%26s%s\n\n", "", "Mdtest metadata rates (IOPS) for 1 task/node");
}


#
# 2.1 The column headers
#


@names = keys(%case);
#print Dumper(\@names);

for ( $i = 0; ($i <= $#names) && ($n_files == 1); $i++) {

    $name = $names[$i];
    if ( $i < $#names ) {
	printf ("%-9s%s", $name, ",");
    }
    else {
	printf ("%-14s%s", $name, ",");
    }

}


@keys = sort keys(%data);

for ( $i = 0;  ($i <= $#keys) && ($n_files == 1); $i++) {

    $key = $keys[$i];
    if ( $i < $#keys ) {
	printf ("%-11s%s", $key, ",");
    }
    else {
	printf ("%-11s%s", $key, "\n");
    }

}




#
#  2.2 The data 
#

#foreach $key  ( sort keys(%data) ) {
#    print $key, "\t", $data{$key}, "\n";
#}



for ( $i = 0;  $i <= $#names; $i++) {

    $name  = $names[$i];
    $val   = $case{$name};
    chomp($val);


    if ( $i < $#names ) {
	printf ("%-9d%s", $val, ",");
    }
    else {
	printf ("%-14d%s", $val, ",");
    }
}



for ( $i = 0;  $i <= $#keys; $i++) {

    $key  = $keys[$i];
    $val  = $data{$key};
    chomp($data);

    if ( $i < $#keys ) {
	printf ("%-11d%s", $val, ",");
    }
    else {
	printf ("%-11d%s", $val, "\n");
    }

}


close FILE;


} # foreach $file ( @files ) { ... }



